package sz.com.postoffice.jxyzwgcase.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import io.swagger.annotations.Api;
import sz.com.postoffice.jxyzwgcase.utils.ExportWord;
import sz.com.postoffice.jxyzwgcase.utils.ZipUtil;

/**
 * @ClassName UserContriller
 * @Deacription TODO
 * @Author 王盟
 * @Date 2020/10/23 13:58
 * @Version 1.0
 **/
@Api(value = "Word导出服务", tags = "{ Word导出服务 }")
@RestController
@RequestMapping(value = "/wordExport")
public class WordController {

	@RequestMapping(value = "/exportWord", method= RequestMethod.GET)
    public void getWord(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        int date = 44145;
        for (int i=0;i < 3;i++) {
        	 Map<String, Object> map = new HashMap<>();
             map.put("name", "name"+i);
             map.put("address", "address"+i);
             map.put("date", date++);
             map.put("aa", "aa"+i);
             map.put("bb", "bb"+i);
             map.put("cc", "cc"+i);
             map.put("dd", "dd"+i);
             ArrayList<Map<String, Object>> list1 = new ArrayList<>();
             for (int j=0;j < 3;j++) {
            	 Map<String, Object> map1 = new HashMap<>();
            	 map1.put("date", date++);
            	 map1.put("aa", "aa"+j);
            	 map1.put("bb", "bb"+j);
            	 map1.put("cc", "cc"+j);
            	 map1.put("dd", "dd"+j);
            	 list1.add(map1);
             }
             map.put("child", list1);
             list.add(map);
        }
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("dataList", list);
        String nowTimeStr = "" +new Date().getTime();
        String fileDir = File.separator + "var" + File.separator + "files" + File.separator + nowTimeStr;
        ExportWord word = new ExportWord("UTF-8");
        System.out.println(jsonMap);
        word.exportDoc(response, "test.ftl", jsonMap);
        ZipUtil book = new ZipUtil(fileDir+".zip");
        book.compress(fileDir);
        
        File f = new File(fileDir+".zip");
        OutputStream out = response.getOutputStream();
        if (!f.exists()) {
            response.setCharacterEncoding("UTF-8");
            out.write("文件找不到".getBytes("UTF-8"));
            out.flush();
            return;
        }
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
        byte[] buf = new byte[1024];
        int len = 0;
        String tarFileName = nowTimeStr+".zip";
        response.reset(); // 非常重要
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename=" + tarFileName);

        while ((len = br.read(buf)) > 0)
            out.write(buf, 0, len);
        br.close();
        out.close();
        
    }
}
